package com.itcast.springboot.waste_recovery.controller;


import com.itcast.springboot.request.EvaluateReq;
import com.itcast.springboot.request.OrderManage;
import com.itcast.springboot.response.MyOrderRes;
import com.itcast.springboot.response.StaffOrderRes;
import com.itcast.springboot.waste_recovery.common.CommonResult;
import com.itcast.springboot.waste_recovery.common.CommonResultCode;
import com.itcast.springboot.waste_recovery.domain.Order;
import com.itcast.springboot.waste_recovery.mapper.OrderMapper;
import com.itcast.springboot.waste_recovery.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-23
 */
@RestController
@RequestMapping("/waste_recovery/order")
@Slf4j
@Api(tags = "订单类接口")
public class OrderController {
    @Resource(name = "orderServiceImpl")
    OrderService orderService;

    @PostMapping("/customer/{user_id}")
    @ApiOperation(value = "新增订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "order", value = "订单")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult insertOrder(@PathVariable(value = "user_id") String userId,
                                     @RequestBody Order order){
        return null;
    }

    @GetMapping("/customer/my_orders")
    @ApiOperation(value = "我的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "order_status", value = "订单状态")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult getMyOrders(@RequestParam(value = "user_id") Integer userId,
                                     @RequestParam(value = "order_status", required = false) Integer orderStatus){
        List<MyOrderRes> myOrders = orderService.getMyOrders(userId, orderStatus);
        return new CommonResult(CommonResultCode.CommonSuccess, myOrders);
    }

    @PutMapping("/customer/evaluate/{order_id}/{order_service_evaluation}/{order_attitude_evaluation}")
    @ApiOperation(value = "评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_id", value = "订单id"),
            @ApiImplicitParam(name = "order_service_evaluation", value = "订单的服务评论"),
            @ApiImplicitParam(name = "order_attitude_evaluation", value = "订单的态度评论")
    })
    @RequiresPermissions("user-operated")       //拥有 user-operated 才可以访问
    public CommonResult evaluate(@PathVariable(value = "order_id") Integer orderId,
                                 @PathVariable(value = "order_service_evaluation") Integer orderServiceEvaluation,
                                 @PathVariable(value = "order_attitude_evaluation") Integer orderAttitudeEvaluation
                                 ){
        Integer result = orderService.evaluate(orderId, orderServiceEvaluation, orderAttitudeEvaluation);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    /**
     * 员工接口设计
     */
    @GetMapping("staff/staff_order/{staff_id}/{order_status}")
    @ApiOperation(value = "员工端订单展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staff_id", value = "员工id"),
            @ApiImplicitParam(name = "order_status", value = "订单状态")
    })
    @RequiresPermissions("staff-operated")       //拥有 staff-operated 才可以访问
    public CommonResult getStaffOrder(@PathVariable(value = "staff_id") Integer staffId,
                                      @PathVariable(value = "order_status") Integer orderStatus){
        List<StaffOrderRes> staffOrderResList = orderService.getStaffOrder(staffId, orderStatus);
        return new CommonResult(CommonResultCode.CommonSuccess, staffOrderResList);
    }

    @PutMapping("staff/take_order/{order_id}")
    @ApiOperation(value = "接单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_id", value = "订单id")
    })
    public CommonResult takeOrder(@PathVariable(value = "order_id") Integer orderId){
        Integer result = orderService.takeOrder(orderId);
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @PutMapping("verity_psw")
    @ApiOperation(value = "校验密码：1代表密码正确；-1代表密码错误")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id", value = "用户id"),
            @ApiImplicitParam(name = "password", value = "用户密码")
    })
    @RequiresPermissions("staff-operated")       //拥有 staff-operated 才可以访问
    public CommonResult verityPsw(@RequestParam(value = "userId") Integer userId,
                                  @RequestParam(value = "password") String userPassword){
        Integer result = orderService.verityPsw(userId, userPassword);
        return new CommonResult(CommonResultCode.CommonSuccess, result);
    }

    @GetMapping("evaluate/{order_id}")
    @ApiOperation(value = "查看评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_id", value = "订单id")
    })
    public CommonResult getEvaluate(@PathVariable(value = "order_id") Integer orderId){
        Map<String, Integer> map = orderService.getEvaluate(orderId);
        return new CommonResult(CommonResultCode.CommonSuccess, map);
    }

}

